package com.ruoyi.common.redis;

import redis.clients.jedis.JedisPubSub;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

import com.ruoyi.common.constant.RedisConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.system.coupon.usercoupon.domain.UserCoupon;
import com.ruoyi.project.system.coupon.usercoupon.service.IUserCouponService;
import com.ruoyi.project.system.goods.goods.domain.GoodsGoods;
import com.ruoyi.project.system.goods.goods.service.IGoodsGoodsService;
import com.ruoyi.project.system.goods.product.domain.GoodsProduct;
import com.ruoyi.project.system.goods.product.service.IGoodsProductService;
import com.ruoyi.project.system.order.order.domain.OrderOrder;
import com.ruoyi.project.system.order.order.dto.OrderOrderDto;
import com.ruoyi.project.system.order.order.enums.OrderStatus;
import com.ruoyi.project.system.order.order.service.IOrderOrderService;
import com.ruoyi.project.system.order.ordergoods.dto.OrderGoodsDto;
import com.ruoyi.project.system.order.ordergoods.service.IOrderGoodsService;
import com.ruoyi.project.system.shopconfig.domain.ShopConfig;
import com.ruoyi.project.system.shopconfig.service.IShopConfigService;

/**
 * 功能：
 *
 * @author：LCL
 * @create：2020/12/16 9:17
 * @version：2020 Version：1.0
 * @company：小牛科技 Created with IntelliJ IDEA
 */
@Component
public class KeyExpiredListener extends KeyExpirationEventMessageListener {

	@Autowired
	private IGoodsGoodsService goodsGoodsService;
	@Autowired
	private RedisTemplate redisTemplate;
	@Autowired
	private IOrderOrderService orderOrderService;
	@Autowired
	private IUserCouponService userCouponService;
	@Autowired
	private IOrderGoodsService orderGoodsService;
	@Autowired
	private IGoodsProductService goodsProductService;
	@Autowired
	private IShopConfigService shopConfigService;

	public KeyExpiredListener(RedisMessageListenerContainer listenerContainer) {
		super(listenerContainer);
	}


	@Override
	public void onMessage(Message messa, byte[] pattern) {
		System.out.println(messa.toString()+" : "+pattern.toString());
		String message = messa.toString();
		//判断过期key是否是需要监听的
		if(StringUtils.isBlank(message)){
			return;
		}
		if(message.endsWith(RedisConstants.getSuffix())){
			//查询商品信息
			String goodsId = message.replace(RedisConstants.getSuffix(), "");
			GoodsGoods goodsGoods = this.goodsGoodsService.selectGoodsGoodsById(goodsId);
			if(goodsGoods==null){
				System.out.println("查询不到:"+goodsId+"的商品,message:"+message);
				return;
			}
			//更新商品信息
			GoodsGoods update = new GoodsGoods();
			update.setId(goodsId);
			String key = goodsGoods.getId()+RedisConstants.getSuffix();
			redisTemplate.delete(key);
			switch (goodsGoods.getStatus()){
				case 0:
					update.setStatus(2);
					redisTemplate.opsForValue().set(key,goodsGoods.getId());
					ShopConfig shopConfig = this.shopConfigService.findAll().get(0);
					redisTemplate.expire(key,shopConfig.getGoodsTime(),TimeUnit.SECONDS);
					break;
				case 1:
					update.setStatus(5);
					break;
				case 2:
					update.setStatus(5);
					break;
			}
			if(update.getStatus()!=null){
				this.goodsGoodsService.updateGoodsGoods(update);
			}
		}
		if(message.endsWith(RedisConstants.getOrderSuffix())){
			String id = message.replace(RedisConstants.getOrderSuffix(), "");
			//查询订单是否存在
			OrderOrder orderOrder = this.orderOrderService.selectOrderOrderById(id);
			if(orderOrder==null){
				return ;
			}
			if(!orderOrder.getStatus().equals(OrderStatus.TO_BE_PAID.getValue())){
				return ;
			}
			OrderOrder order = new OrderOrder();
			order.setId(id);
			order.setStatus(OrderStatus.CANCEL.getValue());
			//判断是否使用优惠券
			if(orderOrder.getUserCouponId()!=null){
				order.setUserCouponNull("1");
			}
			this.orderOrderService.updateOrderOrder(order);
			//判断优惠券是否被使用 没有的话 就修改为未使用
			if(orderOrder.getUserCouponId()!=null){
				OrderOrder select = new OrderOrder();
				select.setUserCouponId(orderOrder.getUserCouponId());
				List<OrderOrderDto> orderOrderDtos = this.orderOrderService.selectOrderOrderList(select);
				if(orderOrderDtos==null){
					UserCoupon update = new UserCoupon();
					update.setId(orderOrder.getUserCouponId());
					update.setStatus(0);
					this.userCouponService.updateUserCoupon(update);
				}
			}
			//释放订单下的产品库存
			List<OrderGoodsDto> listByOrderId = this.orderGoodsService.findListByOrderId(id);
			for(OrderGoodsDto orderGoodsDto:listByOrderId){
				GoodsProduct product = this.goodsProductService.selectGoodsProductById(orderGoodsDto.getProductId());
				//判断规格是否存在 以及 规格是否已删除
				if(product==null||product.getStatus().equals(1)){
					continue;
				}
				//修改规格库存
				GoodsProduct goodsProduct = new GoodsProduct();
				goodsProduct.setStock(product.getStock()+orderGoodsDto.getTotal().intValue());
				goodsProduct.setId(orderGoodsDto.getProductId());
				this.goodsProductService.updateGoodsProduct(goodsProduct);

				//修改产品库存
				GoodsGoods goods = this.goodsGoodsService.selectGoodsGoodsById(orderGoodsDto.getGoodsId());

				GoodsGoods goodsGoods = new GoodsGoods();
				goodsGoods.setId(orderGoodsDto.getGoodsId());
				goodsGoods.setStock(goods.getStock()+orderGoodsDto.getTotal());
				this.goodsGoodsService.updateGoodsGoods(goodsGoods);
			}

		}
	}
}
